package com.wtb.thread;

import java.util.concurrent.BlockingQueue;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.renren.api.client.RenrenApiClient;
import com.renren.api.client.param.impl.AccessToken;
import com.wtb.domain.DataCache;
import com.wtb.pub.WtbException;
import com.wtb.rr.domain.RrOauth;

public class RrWriteThread implements Runnable {
	private static final Log log = LogFactory.getLog(RrWriteThread.class);
	private BlockingQueue<RrOauth> rrQueue;			//人人状态发送队列
	private RenrenApiClient client = RenrenApiClient.getInstance();
	public RrWriteThread(BlockingQueue<RrOauth> rrQueue ) {
		this.rrQueue = rrQueue;
	}
	public void run() {
		log.info("人人写线程已启动");
		while (true) {
			RrOauth rrOauth = null;
			try {
				rrOauth = rrQueue.take();
				log.debug("----------------------------------------------------"+Thread.currentThread().getName()+"正在运行！");
				if (rrOauth.getImageUrl() == null || rrOauth.getImageUrl().equals("")) {
					client.getStatusService().setStatus(rrOauth.getMessage(),new AccessToken(rrOauth.getAccess_token()));
				}else {
					client.getPhotoService().uploadURLImg(0, rrOauth.getImageUrl(), rrOauth.getMessage(),new AccessToken(rrOauth.getAccess_token()));
				}
				DataCache.qqlastTime.replace(rrOauth.getRef(), rrOauth.getLastTime());
				DataCache.countIncrement();
			} catch (Exception e) {
				log.error("发送人人状态失败：", e);
				throw new WtbException("发送人人状态失败", e);
			}
		}
	}
}
